package bill_management

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"tao-admin/utils"
)

type BillView struct {
	BillService *BillService
}

func NewBillView() *BillView {
	return &BillView{
		BillService: NewBillService(),
	}
}

func (v *BillView) searchBillView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	planInfos, total, err := v.BillService.GetBillInfos(ctx)
	if err != nil {
		resp := fmt.Sprintf("error:%v", err)
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": resp,
		}
	} else {
		res.Data = map[string]interface{}{
			"items": planInfos,
			"total": total,
		}
	}
	ctx.JSON(200, res)
}

func (v *BillView) createBillView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := v.BillService.CreateBillItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (v *BillView) updateBillView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := v.BillService.UpdateBillItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (v *BillView) deleteBillView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := v.BillService.DeleteBillItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (v *BillView) detailBillView(ctx *gin.Context) {}
